using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

namespace Project
{
    class TemperatureScheduleWindow:Window
    {
        private Window temperatureScheduleWindow;
        private Canvas canvas = new Canvas();
        private Text text_temp;
        private int temperature = 20;
        private int desiredTemp;

        private string temp_rule;

        public TemperatureScheduleWindow()
        {
            temperatureScheduleWindow = new Window();
            temperatureScheduleWindow.Width = SystemMetrics.ScreenWidth;
            temperatureScheduleWindow.Height = SystemMetrics.ScreenHeight;

            temp_rule = Drivers.getInstance().tempRules;
            CreateWindow();
        }

        public void CreateWindow()
        {
            if (temp_rule != "")
            {
                string[] rule_parts = temp_rule.Split('\\');
                string id = rule_parts[0];
                desiredTemp = int.Parse(rule_parts[1]);
                Text text_rule = new Text(Resources.GetFont(Resources.FontResources.NinaB), "Desired Temperature : " + desiredTemp);
                Canvas.SetTop(text_rule, 0);
                Canvas.SetLeft(text_rule, 0);
                canvas.Children.Add(text_rule);

                SimpleButton button_delete = new SimpleButton(new Image(Resources.GetBitmap(Resources.BitmapResources.delete)), 30, 30);
                Canvas.SetTop(button_delete, 0);
                Canvas.SetRight(button_delete, 0);
                canvas.Children.Add(button_delete);
            }
            else
            {
                Text text_noRule = new Text(Resources.GetFont(Resources.FontResources.NinaB), "There is no a temperature rule");
                Canvas.SetTop(text_noRule, 0);
                Canvas.SetLeft(text_noRule, 0);
                canvas.Children.Add(text_noRule);
            }
            text_temp = new Text(Resources.GetFont(Resources.FontResources.NinaB), temperature.ToString());
            text_temp.HorizontalAlignment = HorizontalAlignment.Center;
            text_temp.VerticalAlignment = VerticalAlignment.Center;
            Panel pan_temp = new Panel();
            pan_temp.Children.Add(text_temp);
            Canvas.SetTop(pan_temp, 50);
            Canvas.SetLeft(pan_temp, 0);
            canvas.Children.Add(pan_temp);

            Image image_temp_up = new Image(Resources.GetBitmap(Resources.BitmapResources.red_arrow_up));
            image_temp_up.HorizontalAlignment = HorizontalAlignment.Center;
            image_temp_up.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_temp_up = new SimpleButton(image_temp_up, 55, 55);
            button_temp_up.BackColor = Colors.White;
            Canvas.SetTop(button_temp_up, 50);
            Canvas.SetRight(button_temp_up, 0);
            canvas.Children.Add(button_temp_up);

            Image image_temp_down = new Image(Resources.GetBitmap(Resources.BitmapResources.blue_arrow_down));
            image_temp_down.HorizontalAlignment = HorizontalAlignment.Center;
            image_temp_down.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_temp_down = new SimpleButton(image_temp_down, 55, 55);
            button_temp_down.BackColor = Colors.White;
            Canvas.SetTop(button_temp_down, 110);
            Canvas.SetRight(button_temp_down, 0);
            canvas.Children.Add(button_temp_down);

            Image image_accept = new Image(Resources.GetBitmap(Resources.BitmapResources.accept));
            image_accept.HorizontalAlignment = HorizontalAlignment.Center;
            image_accept.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_accept = new SimpleButton(image_accept, temperatureScheduleWindow.Width / 2, temperatureScheduleWindow.Height / 5);
            button_accept.BackColor = Colors.White;
            Canvas.SetBottom(button_accept, 0);
            Canvas.SetLeft(button_accept, 0);
            canvas.Children.Add(button_accept);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, temperatureScheduleWindow.Width / 2, temperatureScheduleWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_accept.Click += new EventHandler(buttonAccept_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);
            button_temp_up.Click += new EventHandler(buttonUp_Click);
            button_temp_down.Click += new EventHandler(buttonDown_Click);

            temperatureScheduleWindow.Child = canvas;
            temperatureScheduleWindow.Visibility = Visibility.Visible;
        }

        void buttonAccept_Click(object sender, EventArgs evt)
        {
            Drivers.getInstance().tempRules = Guid.NewGuid().ToString().ToUpper() + '\\' + temperature.ToString();
            temperatureScheduleWindow.Close();
        }

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            temperatureScheduleWindow.Close();
        }

        void buttonUp_Click(object sender, EventArgs evt)
        {
            temperature++;
            text_temp.TextContent = "" + temperature;
        }

        void buttonDown_Click(object sender, EventArgs evt)
        {
            temperature--;
            text_temp.TextContent = "" + temperature;
        }
    }
}
